﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Devsoft.Utilities.Extentions
{
    public static class EnumerableExtension
    {
        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> list,Action<T>  action)
        {
            foreach (var item in list)
            {
                action(item);
            }
            return list;
        }

        public static IList<T> Shuffle<T>(this IEnumerable<T> list)
        {
            return list.ToList().Shuffle();
        }
        public static IList<T> Shuffle<T>(this IList<T> list)
        {
            var rng = new Random();
            var n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
            return list;
        }

        public static List<T> Delete<T>(this List<T> src,Func<T,bool> predicate )
        {
            var objToDetelet = src.Where(predicate).ToList();
            foreach (var obj in objToDetelet)
            {
                src.Remove(obj);
            }
            return src;
        }

        public static List<T> Remove<T>(this List<T> src,Func<T,bool> predicate )
        {
            return Delete(src, predicate);
        }
    }
}
